var _Page;

function _defineProperty(t, a, o) {
    return a in t ? Object.defineProperty(t, a, {
        value: o,
        enumerable: !0,
        configurable: !0,
        writable: !0
    }) : t[a] = o, t;
}

var api = require("../../../api.js"), app = getApp();

Page((_defineProperty(_Page = {
    data: {
        quick_list: [],
        goods_list: [],
        carGoods: [],
        currentGood: {},
        checked_attr: [],
        checkedGood: [],
        attr_group_list: [],
        goods: {
            price: 0,
            num: 0,
            use_attr: 1
        },
        check_goods_price: 0,
        showModal: !1,
        checked: !1,
        cat_checked: !1,
        color: "",
        total: {
            total_price: 0,
            total_num: 0
        }
    },
    onLoad: function(t) {
        app.pageOnLoad(this), this.setData({
            store: wx.getStorageSync("store")
        });
    },
    onShow: function() {
        app.pageOnShow(this), this.loadData();
    },
    onHide: function() {
        app.pageOnHide(this);
        var t = this, a = {
            quick_list: t.data.quick_list,
            carGoods: t.data.carGoods,
            total: t.data.total,
            quick_hot_goods_lists: t.data.quick_hot_goods_lists,
            checked_attr: t.data.checked_attr
        };
        wx.setStorageSync("item", a);
    },
    onUnload: function() {
        app.pageOnUnload(this);
        var t = this, a = {
            quick_list: t.data.quick_list,
            carGoods: t.data.carGoods,
            total: t.data.total,
            quick_hot_goods_lists: t.data.quick_hot_goods_lists,
            checked_attr: t.data.checked_attr
        };
        wx.setStorageSync("item", a);
    },
    loadData: function(t) {
        var d = this, n = wx.getStorageSync("item");
        d.setData({
            total: n.total ? n.total : {
                total_num: 0,
                total_price: 0
            },
            carGoods: n.carGoods ? n.carGoods : []
        }), wx.showLoading({
            title: "加载中"
        }), app.request({
            url: api.quick.quick,
            success: function(t) {
                if (wx.hideLoading(), 0 == t.code) {
                    var a = t.data.list, o = [], r = [];
                    for (var i in a) if (0 < a[i].goods.length) for (var e in r.push(a[i]), a[i].goods) {
                        for (var s in n.carGoods) n.carGoods[s].goods_id === parseInt(a[i].goods[e].id) && (a[i].goods[e].num = a[i].goods[e].num ? a[i].goods[e].num : 0, 
                        a[i].goods[e].num += n.carGoods[s].num);
                        parseInt(a[i].goods[e].hot_cakes) && o.push(a[i].goods[e]);
                    }
                    d.setData({
                        quick_hot_goods_lists: o,
                        quick_list: r
                    });
                }
            }
        });
    },
    get_goods_info: function(t) {
        var a = this, o = a.data.carGoods, r = a.data.total, i = a.data.quick_hot_goods_lists, e = a.data.quick_list, s = {
            carGoods: o,
            total: r,
            quick_hot_goods_lists: i,
            check_num: a.data.check_num,
            quick_list: e
        };
        wx.setStorageSync("item", s);
        var d = t.currentTarget.dataset.id;
        wx.navigateTo({
            url: "/pages/goods/goods?id=" + d + "&quick=1"
        });
    },
    selectMenu: function(t) {
        var a = t.currentTarget.dataset, o = this.data.quick_list;
        if ("hot_cakes" == a.tag) for (var r = !0, i = o.length, e = 0; e < i; e++) o[e].cat_checked = !1; else {
            var s = a.index;
            for (i = o.length, e = 0; e < i; e++) o[e].cat_checked = !1, o[e].id == o[s].id && (o[e].cat_checked = !0);
            r = !1;
        }
        this.setData({
            toView: a.tag,
            selectedMenuId: a.id,
            quick_list: o,
            cat_checked: r
        });
    },
    onShareAppMessage: function(t) {
        var a = this;
        return {
            path: "/pages/quick-purchase/index/index?user_id=" + wx.getStorageSync("user_info").id,
            success: function(t) {
                share_count++, 1 == share_count && app.shareSendCoupon(a);
            }
        };
    },
    jia: function(t) {
        var a = this, o = t.currentTarget.dataset, r = a.data.quick_list;
        for (var i in r) for (var e in r[i].goods) if (parseInt(r[i].goods[e].id) === parseInt(o.id)) {
            var s = r[i].goods[e].num;
            if (r[i].goods[e].num = s ? ++s : 1, s > JSON.parse(r[i].goods[e].attr)[0].num) return wx.showToast({
                title: "商品库存不足",
                image: "/images/icon-warning.png"
            }), void --s;
            var d = a.data.carGoods, n = 1, c = o.price ? o : r[i].goods[e].price;
            for (var _ in d) {
                if (d[_].goods_id === parseInt(o.id) && 1 === d[_].attr.length) {
                    n = 0, d[_].num += 1, d[_].goods_price = (d[_].num * d[_].price).toFixed(2);
                    break;
                }
                if (d[_].price == parseFloat(t.currentTarget.dataset.price)) {
                    n = 0, d[_].num += 1, d[_].goods_price = (d[_].num * d[_].price).toFixed(2);
                    break;
                }
            }
            if (1 === n || 0 === d.length) {
                var u = JSON.parse(r[i].goods[e].attr), g = [];
                g.push(u[0].attr_list[0]), d.push({
                    goods_id: parseInt(r[i].goods[e].id),
                    attr: g,
                    goods_name: r[i].goods[e].name,
                    goods_price: c,
                    num: 1,
                    price: c
                });
            }
        }
        a.setData({
            carGoods: d,
            quick_list: r
        }), a.carStatistics(), a.quickHotStatistics(o.id, "jia");
    },
    jian: function(t) {
        var a = this, o = t.currentTarget.dataset, r = a.data.quick_list;
        for (var i in r) for (var e in r[i].goods) if (parseInt(r[i].goods[e].id) === parseInt(o.id)) {
            var s = 0 < r[i].goods[e].num ? --r[i].goods[e].num : r[i].goods[e].num;
            if ((r[i].goods[e].num = s) > JSON.parse(r[i].goods[e].attr)[0].num) return wx.showToast({
                title: "商品库存不足",
                image: "/images/icon-warning.png"
            }), void ++s;
            var d = a.data.carGoods;
            for (var n in d) {
                o.price ? o.price : r[i].goods[e].price;
                if (d[n].goods_id === parseInt(o.id) && 1 === d[n].attr.length) {
                    0 < d[n].num && (d[n].num -= 1, d[n].goods_price = (d[n].num * d[n].price).toFixed(2));
                    break;
                }
                if (d[n].price == parseFloat(t.currentTarget.dataset.price)) {
                    0 < d[n].num && (d[n].num -= 1, d[n].goods_price = (d[n].num * d[n].price).toFixed(2));
                    break;
                }
            }
        }
        a.setData({
            carGoods: d,
            quick_list: r
        }), a.carStatistics(), a.quickHotStatistics(o.id, "jian");
    },
    carStatistics: function() {
        var t = this.data.carGoods, a = 0, o = 0;
        for (var r in t) a += t[r].num, o += parseFloat(t[r].goods_price);
        var i = {
            total_num: a,
            total_price: o
        };
        0 === a && this.hideGoodsModel(), this.setData({
            total: i
        });
    },
    quickHotStatistics: function(t, a) {
        var o = this.data.quick_hot_goods_lists;
        for (var r in o) parseInt(o[r].id) === parseInt(t) && ("jia" === a && (o[r].num += 1), 
        "jian" === a && (o[r].num = 0 < o[r].num ? --o[r].num : o[r].num));
        this.setData({
            quick_hot_goods_lists: o
        });
    },
    tianjia: function(t) {
        this.jia(t);
    },
    jianshao: function(t) {
        this.jian(t);
    },
    showDialogBtn: function(t) {
        var a = this, o = t.currentTarget.dataset;
        app.request({
            url: api.default.goods,
            data: {
                id: o.id
            },
            success: function(t) {
                0 == t.code && (a.setData({
                    currentGood: t.data,
                    goods_name: t.data.name,
                    attr_group_list: t.data.attr_group_list,
                    showModal: !0
                }), a.resetData(), a.updateData());
            }
        });
    },
    resetData: function() {
        this.setData({
            checked_attr: [],
            check_num: 0,
            check_goods_price: 0,
            goods: {
                price: "0.00"
            }
        });
    },
    updateData: function() {
        var t = this.data.currentGood, a = this.data.carGoods, o = JSON.parse(t.attr), r = t.attr_group_list;
        for (var i in o) {
            var e = [];
            for (var s in o[i].attr_list) e.push(o[i].attr_list[s].attr_id);
            for (var d in a) {
                var n = [];
                for (var c in a[d].attr) n.push(a[d].attr[c].attr_id);
                if (e.sort().join() === n.sort().join()) {
                    for (var _ in r) for (var u in r[_].attr_list) for (var g in e) {
                        if (parseInt(r[_].attr_list[u].attr_id) === parseInt(e[g])) {
                            r[_].attr_list[u].checked = !0;
                            break;
                        }
                        r[_].attr_list[u].checked = !1;
                    }
                    var p = {
                        price: a[d].price
                    };
                    return void this.setData({
                        attr_group_list: r,
                        check_num: a[d].num,
                        check_goods_price: a[d].goods_price,
                        checked_attr: e,
                        goods: p
                    });
                }
            }
        }
    }
}, "updateData", function() {
    var t = this.data.currentGood, a = this.data.carGoods, o = JSON.parse(t.attr), r = t.attr_group_list;
    for (var i in o) {
        var e = [];
        for (var s in o[i].attr_list) e.push(o[i].attr_list[s].attr_id);
        for (var d in a) {
            var n = [];
            for (var c in a[d].attr) n.push(a[d].attr[c].attr_id);
            if (e.sort().join() === n.sort().join()) {
                for (var _ in r) for (var u in r[_].attr_list) for (var g in e) {
                    if (parseInt(r[_].attr_list[u].attr_id) === parseInt(e[g])) {
                        r[_].attr_list[u].checked = !0;
                        break;
                    }
                    r[_].attr_list[u].checked = !1;
                }
                var p = {
                    price: a[d].price
                };
                return void this.setData({
                    attr_group_list: r,
                    check_num: a[d].num,
                    check_goods_price: a[d].goods_price,
                    checked_attr: e,
                    goods: p
                });
            }
        }
    }
}), _defineProperty(_Page, "checkUpdateData", function(t) {
    var a = this.data.carGoods;
    for (var o in a) {
        var r = [];
        for (var i in a[o].attr) r.push(a[o].attr[i].attr_id);
        r.sort().join() === t.sort().join() && this.setData({
            check_num: a[o].num,
            check_goods_price: a[o].goods_price
        });
    }
}), _defineProperty(_Page, "close_box", function(t) {
    this.setData({
        showModal: !1
    });
}), _defineProperty(_Page, "attrClick", function(t) {
    var a = this, o = t.target.dataset.groupId, r = t.target.dataset.id, i = a.data.attr_group_list, e = a.data.currentGood;
    for (var s in i) if (i[s].attr_group_id == o) for (var d in i[s].attr_list) i[s].attr_list[d].attr_id == r ? i[s].attr_list[d].checked = !0 : i[s].attr_list[d].checked = !1;
    var n = [];
    for (var s in i) for (var d in i[s].attr_list) !0 === i[s].attr_list[d].checked && n.push(i[s].attr_list[d].attr_id);
    var c = JSON.parse(a.data.currentGood.attr), _ = a.data.goods;
    for (var u in c) {
        var g = [];
        for (var p in c[u].attr_list) g.push(c[u].attr_list[p].attr_id);
        if (g.sort().join() === n.sort().join()) {
            if (0 === parseInt(c[u].num)) return void wx.showToast({
                title: "商品库存不足，请选择其它规格或数量",
                image: "/images/icon-warning.png"
            });
            _ = parseFloat(c[u].price) ? {
                price: c[u].price.toFixed(2)
            } : {
                price: e.price.toFixed(2)
            };
        }
    }
    a.resetData(), a.checkUpdateData(n), a.setData({
        attr_group_list: i,
        goods: _,
        checked_attr: n
    });
}), _defineProperty(_Page, "onConfirm", function(t) {
    var a = this, o = a.data.attr_group_list, r = a.data.checked_attr, i = a.data.currentGood, e = a.data.check_num ? ++a.data.check_num : 1;
    if (r.length !== o.length) return wx.showToast({
        title: "请选择规格",
        image: "/images/icon-warning.png"
    }), void --c;
    var s = a.data.quick_list;
    for (var d in s) for (var n in s[d].goods) if (parseInt(s[d].goods[n].id) === parseInt(i.id)) {
        var c = s[d].goods[n].num;
        if (s[d].goods[n].num = c ? ++c : 1, c > JSON.parse(s[d].goods[n].attr)[0].num) return wx.showToast({
            title: "商品库存不足",
            image: "/images/icon-warning.png"
        }), --c, console.log(12), void a.setData({
            check_num: --e
        });
        a.data.total.total_num && a.data.total.total_num;
        var _ = a.data.total.total_price ? a.data.total.total_price : 0;
        1;
        var u = a.data.goods.price;
        _ = (parseFloat(_) + parseFloat(1 * u)).toFixed(2);
        var g = a.data.carGoods, p = 1, h = (parseFloat(u) * e).toFixed(2);
        for (var l in g) {
            var f = [];
            for (var v in g[l].attr) f.push(g[l].attr[v].attr_id);
            if (f.sort().join() === r.sort().join()) {
                p = 0, g[l].num += 1, g[l].goods_price = (parseFloat(u) * g[l].num).toFixed(2);
                break;
            }
        }
        if (1 === p || 0 === g.length) {
            var m = JSON.parse(s[d].goods[n].attr), k = [];
            for (var G in m) {
                var q = [];
                for (var w in m[G].attr_list) q.push(m[G].attr_list[w].attr_id);
                if (q.sort().join() === a.data.checked_attr.sort().join()) {
                    var S = parseFloat(m[G].price) ? m[G].price.toFixed(2) : i.price.toFixed(2);
                    if (k = m[G].attr_list, 0 === parseInt(m[G].num)) return void wx.showToast({
                        title: "商品库存不足，请选择其它规格或数量",
                        image: "/images/icon-warning.png"
                    });
                }
            }
            g.push({
                goods_id: parseInt(s[d].goods[n].id),
                attr: k,
                goods_name: s[d].goods[n].name,
                goods_price: h,
                num: 1,
                price: S
            });
        }
    }
    a.setData({
        carGoods: g,
        quick_list: s,
        check_goods_price: h,
        check_num: e
    }), a.carStatistics(), a.attrGoodStatistics();
}), _defineProperty(_Page, "preventTouchMove", function() {}), _defineProperty(_Page, "hideModal", function() {
    this.setData({
        showModal: !1
    });
}), _defineProperty(_Page, "guigejian", function(t) {
    var a = this, o = a.data.checked_attr, r = a.data.carGoods, i = a.data.check_num ? --a.data.check_num : 1;
    a.data.currentGood;
    for (var e in r) {
        var s = [];
        for (var d in r[e].attr) s.push(r[e].attr[d].attr_id);
        if (s.sort().join() === o.sort().join()) return 0 < r[e].num && (r[e].num -= 1, 
        r[e].goods_price = (r[e].num * parseFloat(r[e].price)).toFixed(2)), a.setData({
            carGoods: r,
            check_goods_price: r[e].goods_price,
            check_num: i
        }), a.carStatistics(), void a.attrGoodStatistics();
    }
}), _defineProperty(_Page, "attrGoodStatistics", function() {
    var t = this, a = t.data.currentGood, o = t.data.carGoods, r = t.data.quick_list, i = t.data.quick_hot_goods_lists, e = 0;
    for (var s in o) o[s].goods_id === a.id && (e += o[s].num);
    for (var s in r) for (var d in r[s].goods) parseInt(r[s].goods[d].id) === a.id && (r[s].goods[d].num = e);
    for (var s in i) parseInt(i[s].id) === a.id && (i[s].num = e);
    t.setData({
        quick_list: r,
        quick_hot_goods_lists: i
    });
}), _defineProperty(_Page, "goodsModel", function(t) {
    this.data.carGoods;
    var a = this.data.goodsModel;
    a ? this.setData({
        goodsModel: !1
    }) : this.setData({
        goodsModel: !0
    });
}), _defineProperty(_Page, "hideGoodsModel", function() {
    this.setData({
        goodsModel: !1
    });
}), _defineProperty(_Page, "clearCar", function(t) {
    var a = this.data.quick_hot_goods_lists, o = this.data.quick_list;
    for (var r in a) for (var i in a[r]) a[r].num = 0;
    for (var e in o) for (var s in o[e].goods) o[e].goods[s].num = 0;
    this.setData({
        goodsModel: !1,
        carGoods: [],
        total: {
            total_num: 0,
            total_price: 0
        },
        check_num: 0,
        quick_hot_goods_lists: a,
        quick_list: o,
        currentGood: [],
        checked_attr: [],
        check_goods_price: 0,
        goods: {}
    }), wx.removeStorageSync("item");
}), _defineProperty(_Page, "buynow", function(t) {
    var a = this.data.carGoods;
    this.data.goodsModel;
    this.setData({
        goodsModel: !1
    });
    for (var o = a.length, r = [], i = [], e = 0; e < o; e++) 0 != a[e].num && (i = {
        goods_id: a[e].goods_id,
        num: a[e].num,
        attr: a[e].attr
    }, r.push(i));
    var s = [];
    s.push({
        mch_id: 0,
        goods_list: r
    }), wx.navigateTo({
        url: "/pages/new-order-submit/new-order-submit?mch_list=" + JSON.stringify(s)
    }), this.clearCar();
}), _Page));